package assists 
{
	import flash.filesystem.File;
	
	import main.ClientConstants;
	
	/**
	 * ...
	 * @author clouree
	 */
	public class URI
	{
		private var _frontAddress:String = "";//前缀地址；
		private var _endAddress:String = "";//后缀地址；
		
		
		public var cityNamePath:String = "";
		public var dpiSize:int = 1;
		
		public var ipPath:String = ClientConstants.SERVER_IP_PATH;
		
		public function set cityPath(str:String):void
		{
			cityNamePath = str;
		}
		
		public function set frontAddress(value:String):void
		{
			_frontAddress = value;
		}
		
		public function set endAddress(value:String):void
		{
			_endAddress = value;
		}
		
		public function getUri(key:String):String
		{
			return _frontAddress + key + _endAddress;
		}
		
		public function getPublicRec(value:String):String
		{
			return getUri("assets/public/" + value + ".png")
		}
		
		public function getXML(value:String):String
		{
			var url:String = checkExist(value + "/design.xml");
			if(url == ""){
				return getUri("http://" + ipPath + "/IWSource/clientResource/Android/assets/" + cityNamePath + "/scenic/weihai/" + value + "/design.xml");
			}
			return getUri(url);
		}
		
		public function getNavigation(value:String):String
		{
			return getUri("assets/navigation/" + value + ".png")
		}
		
		public function getNavigationSwf(picName:String,fileName:String):String
		{
			return getUri("assets/navigation/" + fileName + "/" + picName + ".swf");
		}
		
		public function getNavigationPic(picName:String,fileName:String):String
		{
			var url:String = File.documentsDirectory.resolvePath("iWeiHai" + "/" + cityNamePath + "/scenic/weihai/" + fileName + "/" + picName + ".jpg").nativePath; 
			url = "file://" + url;
			return getUri(url);
		}
		
		public function getNavigationXml(fileName:String):String
		{
			var url:String = File.documentsDirectory.resolvePath("iWeiHai" + "/" + cityNamePath + "/scenic/weihai/" + fileName + "/design.xml").nativePath; 
			url = "file://" + url;
			return getUri(url);
		}
		
		public function getNavigationDetailPic(picName:String,fileName:String):String
		{
			return getUri("assets/navigation/" + fileName + "/detail/" + picName + ".jpg");
		}
		
		public function getNavigationPng(picName:String,fileName:String):String
		{
			return getUri("assets/navigation/" + fileName + "/" + picName + ".png");
		}
		
		public function getNavigationMp3(picName:String,fileName:String):String
		{
			return getUri("assets/navigation/" + fileName + "/mp3/" + picName + ".mp3");
		}
		
		public function getResturantUri(preStr:String,key:String):String                                   //小吃图片位置
		{
			var url:String = checkExist(preStr + key + ".jpg");
			if(url == ""){
				return getUri("http://" + ipPath + "/IWSource/clientResource/Android/assets/" + cityNamePath + "/imageResource/" + preStr + key + ".jpg");
			}
			
			return getUri(url);
		}
		
		public function getHotelUri(preStr:String,key:String):String                                   //小吃图片位置
		{
			var url:String = checkExist(preStr + key + ".jpg");
			if(url == ""){
				return getUri("http://" + ipPath + "/IWSource/clientResource/Android/assets/" + cityNamePath + "/imageResource/" + preStr + key + ".jpg");
			}
			return getUri(url);
		}
		
		public function getScenicShop(preStr:String,key:String):String                                      //景区商店图片位置
		{
			var url:String = checkExist(preStr + key + ".png");
			if(url == ""){
				return getUri("http://" + ipPath + "/IWSource/clientResource/Android/assets/" + cityNamePath + "/imageResource/" + preStr + key + ".jpg");
			}
			
			return getUri(url);
		}
		
		public function getSysXml(key:String):String                                              //系统通用XML文件，语言，城市列表
		{
			return getUri("assets/designXML/" + key + ".xml");
		}
		
		public function getEatingResturantXmlUri(key:String):String                      //小吃XML位置
		{
			//			return getUri("assets/designXML/" + key + ".xml");
			
			var url:String = checkExist(key + ".xml");
			if(url == ""){
				return getUri("http://" + ipPath + "/IWSource/clientResource/Android/assets/" + cityNamePath + "/designXML/" + key + ".xml");
			}
			
			return getUri(url);
		}
		
		public function getScenicShopXml(key:String):String                                  //景区商店，娱乐XML位置
		{
			var url:String = checkExist(key + ".xml");
			if(url == ""){
				return getUri("http://" + ipPath + "/IWSource/clientResource/Android/assets/" + cityNamePath + "/designXML/" + key + ".xml");
			}
			
			return getUri(url);
		}
		
		public function getScenicXml(key:String):String                                           //景区XML位置
		{
			var url:String = checkExist(key + ".xml");
			if(url == ""){
				return getUri("http://" + ipPath + "/IWSource/clientResource/Android/assets/" + cityNamePath + "/designXML/" + key + ".xml");
			}
			
			return getUri(url);
		}
		
		public function getScenicImage(preStr:String,key:String):String                                      //景区推荐路线图片资源位置
		{
			var url:String = checkExist(preStr + key + ".jpg");
			if(url == ""){
				return getUri("http://" + ipPath + "/IWSource/clientResource/Android/assets/" + cityNamePath + "/imageResource/" + preStr + key + ".jpg");
			}		
			return getUri(url);
		}
		
		public function getViewSpotPic(preStr:String,key:String):String
		{
			var url:String = checkExist(preStr + key + ".jpg");
			if(url == ""){
				return getUri("http://" + ipPath + "/IWSource/clientResource/Android/assets/" + cityNamePath + "/imageResource/" + preStr + key + ".jpg");
			}
			return getUri(url);
		}
		
		public function getViewSpotDetailPic(preStr:String,key:String):String
		{

			var url:String = checkExist(preStr + key + ".jpg");
			if(url == ""){
				return getUri("http://" + ipPath + "/IWSource/clientResource/Android/assets/" + cityNamePath + "/imageResource/" + preStr + key + ".jpg");
			}
			return getUri(url);
		}
		
		public function getNewsImg(preStr:String,key:String):String
		{
			var url:String = checkExist(preStr + key + ".jpg");
			if(url == ""){
				return getUri("http://" + ipPath + "/IWSource/clientResource/Android/assets/" + cityNamePath + "/" + preStr + key + ".jpg");
			}
			return getUri(url);
		}
		
		public function getWeiCo(key:String):String
		{
			
			return getUri("assets/weico/" + key + ".png");
		}
		
		//======================================新URI路径
		
		public function getCityZip(str:String):String                                                                         //服务器获取城市压缩包路径
		{
			return getUri("http://" + ipPath + "/IWSource/clientResource/Android/zip/" + str + ".zip");
		}
		
		public function getScenicZip(str:String):String
		{	
			return getUri("http://" + ipPath + "/IWSource/clientResource/Android/zip/" + str + ".zip");
		}
		
		//====================================
		
		public function checkExist(name:String):String
		{
			var directory:File = File.documentsDirectory.resolvePath("iWeiHai" + "/" + cityNamePath + "/"); 
			if(directory.exists == false){
				directory.createDirectory();
				return "";
			}
			
			var allFileList:Array = [];
			
			seeIntoPack(directory);
			
			function seeIntoPack(directory:File):void
			{
				var contents:Array = directory.getDirectoryListing();
				
				for (var i:uint = 0; i < contents.length; i++)  
				{ 
					if(contents[i].isDirectory == true){
						seeIntoPack(contents[i]);          //嵌套
					}else{
						allFileList.push(contents[i]);
					}
				} 
			}
			
			for(var j:int = 0;j < allFileList.length;j++){
				if(allFileList[j].url.indexOf(name) != -1){
					return allFileList[j].url;
				}
			}
			
			return "";
		}
		
	}
}